﻿using AutomatedTestDataManagement.Models;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;

namespace AutomatedTestDataManagement.Controllers
{
    public class PagesFieldController : Controller
    {
        private ApplicationTestDataEntities db = new ApplicationTestDataEntities();

        //
        // GET: /PagesField/

        public ActionResult Index()
        {
            var pagesfields = db.PagesFields.Include(p => p.HTMLField).Include(p => p.Page);
            return View(pagesfields.ToList());
        }

        //
        // GET: /PagesField/Details/5

        public ActionResult Details(int id = 0)
        {
            PagesField pagesfield = db.PagesFields.Find(id);
            if (pagesfield == null)
            {
                return HttpNotFound();
            }
            return View(pagesfield);
        }

        //
        // GET: /PagesField/Create

        public ActionResult Create(int id = 0, int siteID = 0)
        {
            ViewBag.FieldID = new SelectList(db.HTMLFields, "FieldID", "FieldName");
            if (id == 0)
            {
                ViewBag.PageID = new SelectList(db.Pages, "PageID", "PageName");
            }
            else
            {
                ViewBag.PageID = new SelectList(db.Pages, "PageID", "PageName", id);
                ViewBag.SelectedPageID = id;
            }
            if (siteID != 0)
            {
                ViewBag.SiteID = siteID;
            }
            return View();
        }

        //
        // POST: /PagesField/Create

        [HttpPost]
        public ActionResult Create(PagesField pagesfield)
        {
            if (ModelState.IsValid)
            {
                db.PagesFields.Add(pagesfield);
                db.SaveChanges();
                if (Request.Form["SiteID"] != null)
                {
                    return RedirectToAction("Edit", "Page", new { id = pagesfield.PageID, siteID = Request.Form["SiteID"] });
                }
                else
                {
                    return RedirectToAction("Edit", "Page", new { id = pagesfield.PageID });
                }
            }
            if (Request.Form["SiteID"] != null)
            {
                ViewBag.SiteID = Request.Form["SiteID"];
            }
            ViewBag.FieldID = new SelectList(db.HTMLFields, "FieldID", "FieldName", pagesfield.FieldID);
            ViewBag.PageID = new SelectList(db.Pages, "PageID", "PageName", pagesfield.PageID);
            return View(pagesfield);
        }

        //
        // GET: /PagesField/Edit/5

        public ActionResult Edit(int id = 0, int siteID = 0)
        {
            PagesField pagesfield = db.PagesFields.Find(id);
            if (pagesfield == null)
            {
                return HttpNotFound();
            }

            if (siteID != 0)
            {
                ViewBag.SiteID = siteID;
            }
            ViewBag.FieldID = new SelectList(db.HTMLFields, "FieldID", "FieldName", pagesfield.FieldID);
            ViewBag.PageID = new SelectList(db.Pages, "PageID", "PageName", pagesfield.PageID);
            return View(pagesfield);
        }

        //
        // POST: /PagesField/Edit/5

        [HttpPost]
        public ActionResult Edit(PagesField pagesfield)
        {
            var siteID = Request.Form["SiteID"];
            if (ModelState.IsValid)
            {
                db.Entry(pagesfield).State = EntityState.Modified;
                db.SaveChanges();
                if (siteID != null)
                {
                    return RedirectToAction("Edit", "Page", new { id = pagesfield.PageID, siteID = siteID });
                }
                else
                {
                    return RedirectToAction("Edit", "Page", new { id = pagesfield.PageID });
                }
            }
            if (siteID != null) { ViewBag.SiteID = siteID; }
            ViewBag.FieldID = new SelectList(db.HTMLFields, "FieldID", "FieldName", pagesfield.FieldID);
            ViewBag.PageID = new SelectList(db.Pages, "PageID", "PageName", pagesfield.PageID);
            return View(pagesfield);
        }

        //
        // GET: /PagesField/Delete/5

        public ActionResult Delete(int id = 0, int siteID = 0, int pageID = 0, int fieldID = 0, string returnToView = "Edit")
        {
            PagesField pagesfield = db.PagesFields.Find(id);
            if (pagesfield == null)
            {
                return HttpNotFound();
            }

            if (siteID != 0) { ViewBag.SiteID = siteID; }
            if (pageID != 0) { ViewBag.PageID = pageID; }
            if (fieldID != 0) { ViewBag.FieldID = fieldID; }
            ViewBag.ReturnToView = returnToView;
            return View(pagesfield);
        }

        //
        // POST: /PagesField/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var siteID = Request.Form["SiteID"];
            var fieldID = Request.Form["FieldID"];
            var returnToView = Request.Form["ReturnToView"];

            PagesField pagesfield = db.PagesFields.Find(id);
            db.PagesFields.Remove(pagesfield);
            db.SaveChanges();
            if (siteID != null)
            {
                return RedirectToAction(returnToView, "Page", new { id = pagesfield.PageID, siteID = siteID });
            }
            else
            {
                if (fieldID == null)
                {
                    return RedirectToAction(returnToView, "Page", new { id = pagesfield.PageID });
                }
                else
                {
                    return RedirectToAction(returnToView, "HTMLField", new { id = fieldID });
                }
            }

        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}